package com.langsin.wanghongxiang;
import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.wanghongxiang.mapper.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MyTest {
    SqlSession sqlSession=null;
    @Test
    public void testUseGoodsLazy()throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname()+","+user.getAddress());
        Thread.sleep(5000);
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);
        }



    }
    @Test
    public void testUseGoods()throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user);
        System.out.println("----------");
        for (Orders orders : user.getOrdersList()) {
            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getGoods());
            }
        }
    }
    @Test
    public void testOrdersUser()throws Exception{
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrdersId(1003);
       //先获取主表的信息
        System.out.println(orders.getOrderId()+"~"+orders.getOrderDate());
        //模拟程序渡过了一段时间
       Thread.sleep(10000);
       //需要获取关联的用户信息
        System.out.println(orders.getUser());


    }
    @Test
    public void testWs()throws Exception{
        Worker worker = new Worker();
        worker.setWid("1554746097917276160");
        worker.setWname("大律师");
        worker.setAge(45);
        worker.setSex("男");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int i = workerMapper.updateWorkerByCondition(worker);
        System.out.println(i);

    }
    @Test
    public void test1M()throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(702);
        System.out.println(user);
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);
        }
    }
    @Test
    public void test3()throws Exception{
        Goods goods = new Goods();
        goods.setGoodsName("%C%");
        goods.setShopPrice(18.0);
        goods.setGoodsStock(900);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.queryByCondition(goods);
        goodsList.forEach(System.out::println);

    }
    @Test
    public void test2()throws Exception{
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(9003);
        ids.add(9008);
        ids.add(9009);
        List<Goods> goods = goodsMapper.queryByIds(ids);
        for (Goods good : goods) {
            System.out.println(good);
        }
        Goods goods1 = goodsMapper.queryByIdAno(9001);
        System.out.println(goods1);

    }
    @Test
    public void testVo()throws Exception{
        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(vo);

        users.forEach(System.out::println);

    }
    @Test
    public void testMap()throws Exception{
        HashMap<String,Object> map=new HashMap<>();
        map.put("roleCode","plain");
        map.put("address","济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserByMap(map);
        users.forEach(System.out::println);

    }
    @Test
    public void testTea()throws Exception{
        Integer tid = 1009;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryById(tid);
        System.out.println(teacher);

    }
    @Test
    public void testUser() throws Exception{
        Integer uid=702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);
    }
    @Test
    public void testTabname()throws Exception{
        String tabName ="worker";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName+"="+counts);
    }
    @Test
    public void testMapperLike1() throws Exception{
        String name ="X";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName2(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }
    @Test
    public void testMapperLike() throws Exception{
        String name ="%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }
    @Test
    public void testMapperGoods() throws Exception{
        Goods goods = new Goods();
        goods.setGoodsName("品胜 typeC数据线");
        goods.setShopPrice(15.88);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int i = goodsMapper.addGoods(goods);
        System.out.println(goods);
    }
    @Test
    public void testMapper3() throws Exception{
        Worker worker = new Worker();
        worker.setWname("警察");
        worker.setAge(30);
        worker.setSex("女");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int i = workerMapper.insertWorker2(worker);
        System.out.println(i+"----->"+worker);
    }
    @Test
    public void testMapper2() throws Exception{
        Worker worker = new Worker();
        //主键 证书自增，字符串uuid，雪花算法（课下自己了解）
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("律师");
        worker.setAge(40);
        worker.setSex("女");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row= workerMapper.insertWorker(worker);
        System.out.println(row);
    }
    @Test
    public void testMapper1() throws Exception{
        //获取mapper接口(dao)的动态代理实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workers = workerMapper.selectByAge(50);
        workers.forEach(System.out::println);
    }
    //每次单元测试之前 都会执行
    @Before
    public void before() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //默认不自动提交
        sqlSession = sqlSessionFactory.openSession();
    }
    //每次单元测试之后 都会执行
    @After
    public void after() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void test() throws Exception
    {
//        Worker worker = sqlSession.selectOne("worker.selectWorker","68c8d109-5e5d-11e9-9dc0-28d244d68b7c");
//        System.out.println(worker);
        //演示一个修改
        Worker worker = new Worker();
        worker.setAge(500);
        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        int row = sqlSession.update("worker.updateAgeByID", worker);
        System.out.println(row);
    }
}
